﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using NetCoreCMS.IServices;
using StackExchange.Redis;

namespace NetCoreCMS.Web.Areas.WebApi.Controllers
{
    public class Get_ShopController : UserController
    {
        public IShopService _shop { get; set; }
        public IFavoriteService _favorite { get; set; }
        [HttpGet]
        public IActionResult Get(int id,double longitude, double latitude)
        {
            longitude = 116.5484941006d;
            latitude = 31.7575372817d;
            string redisName = Request.Host.Host.ToLower().Replace(".", "_") + "_";
            GeoRadiusResult[] shops = ShopGeoHelper.GetGEO(longitude, latitude, 2000, 100,0,redisName,_shop);
            List<DTO.IndexShopModel> listshop = new List<DTO.IndexShopModel>();
            if (shops.Length > 0)
            {
                foreach (var item in shops)
                {
                    listshop.Add(new DTO.IndexShopModel { Distance = double.Parse(item.Distance.ToString()), Shop = _shop.GetModel(Convert.ToInt32(item.Member.ToString().Replace(redisName + "shopgeo_", ""))) });
                }
            }
            var shop = listshop.FirstOrDefault(a => a.Shop.id == id);
            var sys = Common.XMLConfig.Sysconfig.loadConfig(_env.ContentRootPath);
            shop.Shop.img_url = sys.weburl + shop.Shop.img_url;
            if(shop.Shop.albums!=null)
            for (int i = 0; i < shop.Shop.albums.Count; i++)
            {
                    shop.Shop.albums[i].original_path = sys.weburl + shop.Shop.albums[i].original_path;
                    shop.Shop.albums[i].thumb_path = sys.weburl + shop.Shop.albums[i].thumb_path;
            }
            int shoucang = 0;
            try
            {
                var user = GetUserInfo();
                if (user != null)
                {
                    var list = _favorite.GetList(1, 1000, "where user_id=" + user.id, "id desc");
                    if (list.Where(a => a.like_id == id && a.ftype == 3).Count() > 0)
                    {
                        shoucang = 1;
                    }
                }
            }
            catch (Exception)
            {

            }
            return Ok(new { status = 1, data = shop,shoucang=shoucang });
        }
    }
}